layui.define(['layer', 'jquery', 'element', 'toast'], function (exports) {
    "use strict";

    var MOD_NAME = 'popup',
        $ = layui.jquery,
        layer = layui.layer,
        element = layui.element;
    var toast = layui.toast;

    var popup = new function () {

        this.success = function (msg) {
            layer.msg(msg, {
                icon: 1,
                time: 1000
            })
        },
            this.failure = function (msg) {
                layer.msg(msg, {
                    icon: 2,
                    time: 1000
                })
            },
            this.warming = function (msg) {
                layer.msg(msg, {
                    icon: 3,
                    time: 1000
                })
            },
            this.success = function (msg, callback) {
                layer.msg(msg, {
                    icon: 1,
                    time: 1000
                }, callback);
            },
            this.failure = function (msg, callback) {
                layer.msg(msg, {
                    icon: 2,
                    time: 1000
                }, callback);
            },
            this.warning = function (msg, callback) {
                layer.msg(msg, {
                    icon: 3,
                    time: 1000
                }, callback);
            },
            this.toastSuccess = function (msg, title) {
                toast.success({ title: title || '', message: msg });
            },
            this.toastInfo = function (msg, title) {
                toast.info({ title: title || '', message: msg });
            },
            this.toastWarning = function (msg, title) {
                toast.warning({ title: title || '', message: msg });
            },
            this.toastError = function (msg, title) {
                toast.error({ title: title || '', message: msg });
            },
            this.toastQuestion = function (msg, title) {
                toast.question({ title: title || '', message: msg });
            },
            this.toastResult = function (res, title) {
                if (res.code === 0) {
                    toast.success({ title: title || '', message: res.msg });
                } else {
                    toast.error({ title: title || '', message: res.msg });
                }
            },
            this.panelWeb = function (title, actionUrl, area, loadFn) {
                var panel = "modal-panel-" + Math.random().toString(36).substr(2);
                if ($("#" + panel).length === 0) {
                    var modalPanel = '<div style="padding: 5px;" id="' + panel + '">';
                    modalPanel += '</div>';
                    $('#extraContainer').append(modalPanel);
                }
                $('#' + panel).load(actionUrl, function (response, status, xhr) {
                    if (status === 'success') {
                        if (loadFn) {
                            loadFn.call();
                        }
                    }
                });

                var panelWebIndex = layer.open({
                    type: 1,
                    title: title,
                    area: area,
                    isOutAnim: false,
                    resize: false,
                    shade: 0.5,
                    scrollbar: false,
                    content: $('#' + panel),
                    end: function () {
                        $('#' + panel).remove();
                        layer.close(panelWebIndex);
                    }
                });
                return panelWebIndex;
            },
            this.panelWebBtn = function (title, actionUrl, area, yesFn, loadFn) {
                var panel = "modal-panel-" + Math.random().toString(36).substr(2);
                if ($("#" + panel).length === 0) {
                    var modalPanel = '<div style="padding: 5px;" id="' + panel + '">';
                    modalPanel += '</div>';
                    $('#extraContainer').append(modalPanel);
                }
                $('#' + panel).load(actionUrl, function (response, status, xhr) {
                    if (status === 'success') {
                        if (loadFn) {
                            loadFn.call();
                        }
                    }
                });
                var panelWebIndex = layer.open({
                    type: 1,
                    title: title,
                    area: area,
                    isOutAnim: false,
                    resize: false,
                    shade: 0.5,
                    scrollbar: false,
                    btn: ['\u786e\u5b9a', '\u53d6\u6d88'],
                    content: $('#' + panel),
                    end: function () {
                        $('#' + panel).remove();
                        layer.close(panelWebIndex);
                    },
                    yes: function (index, layero) {
                        if (yesFn) {
                            yesFn.call();
                        }
                    }
                });
                return panelWebIndex;
            }
    };
    exports(MOD_NAME, popup);
})
